<style rel="stylesheet/scss" lang="scss" scoped>
.base_attr_wrap{
    display: flex;
    .label{
        flex: 2;
    }
    .select_vals {
        display: flex;
        justify-content: flex-start;
        flex: 8;
        
    }
    .show_desc {
        display: flex;
        justify-content: flex-start;
        flex: 1;
        margin-left: 10px;
    }
}
</style>

<template>
    <div>
        <div class="container">
            <el-card shadow="hover" style="padding:0">

                <el-form ref="searchForm" :inline="true" :model="searchForm" size="medium" label-width="120px">
                    <el-card class="operate_container" shadow="never">
                        <div class="wrapper">
                            <div><i class="el-icon-tickets" style="margin-right: 10px;"></i>数据列表</div>
                            <!-- 操作按钮 -->
                            <div>
                                <el-button type="danger" icon="el-icon-delete" size="medium" circle @click="handleDelete" :disabled="selectionRows.length <= 0"></el-button>
                                <el-button type="primary" icon="el-icon-refresh" size="medium" circle @click="handleResetSearch('searchForm')"></el-button>
                                <el-button type="primary" icon="el-icon-search" size="medium" circle @click="searchVisible = !searchVisible"></el-button>
                            </div>
                        </div>
                    </el-card>

                    <!-- 组合条件检索 -->
                    <el-card class="filter_container" shadow="never" v-show="searchVisible">
                        <div style="margin-top: 15px">
                            <el-form-item label="分类" prop="cateId">
                                <CategoryCascader v-model="searchForm.cateId" ref="category" styles="width: 100%"></CategoryCascader>
                            </el-form-item>
                            <el-form-item label="品牌" prop="brandId">
                                <BrandSelect v-model="searchForm.brandId" ref="brand" styles="width: 100%"></BrandSelect>
                            </el-form-item>
                            <el-form-item label="状态" prop="publishStatus">
                                <el-select v-model="searchForm.publishStatus" clearable>
                                    <el-option label="新建" :value="0"></el-option>
                                    <el-option label="上架" :value="1"></el-option>
                                    <el-option label="下架" :value="2"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="检索" prop="keyword">
                                <el-input v-model="searchForm.keyword" clearable @clear="handleResetSearch('searchForm')"></el-input>
                            </el-form-item>
                            
                            <el-form-item label=" ">
                                <el-button type="primary" @click="handleSearch()" size="medium">搜索</el-button>
                                <el-button @click="handleResetSearch('searchForm')" size="medium">重置</el-button>
                            </el-form-item>
                        </div>
                    </el-card>
                </el-form>

                <!-- 表格 -->
                <el-table
                    ref="table" size="small" empty-text="暂无数据"
                    element-loading-text="给我一点时间"
                    fit stripe border highlight-current-row
                    v-loading="dataListLoading"
                    :data="dataList"
                    :header-cell-style="{'padding-top': '10px', 'height': '50px', 'color': '#515A6E', 'background-color': '#f8f8f9'}"
                    @selection-change="handleSelection">
                    <el-table-column type="selection" width="55" align="center"></el-table-column>
                    <el-table-column type="index" width="55" align="center"></el-table-column>

                    <el-table-column label="分类" prop="categoryName" align="center"/>
                    <el-table-column label="品牌" prop="brandName" align="center"/>
                    <el-table-column label="商品名称" prop="spuName" align="center"/>
                    <el-table-column label="商品描述" prop="spuDescription" align="center"/>
                    <el-table-column label="重量(Kg)" prop="weight" align="center"/>
                    <el-table-column label="状态" prop="publishStatus" align="center">
                        <template slot-scope="scope">
                            <el-tag type="primary" v-if="scope.row.publishStatus == 0">新建</el-tag>
                            <el-tag type="success" v-if="scope.row.publishStatus == 1">上架</el-tag>
                            <el-tag type="info" v-if="scope.row.publishStatus == 2">下架</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" prop="ctime" align="center"/>
                    <el-table-column label="更新时间" prop="utime" align="center"/>

                    <el-table-column label="操作" width="120px" align="center" fixed="right">
                        <template slot-scope="scope">
                            <div class="table_action">
                                
                                <el-button v-if="scope.row.publishStatus === 0" type="text" size="small" @click="handleProductUp(scope.row.id)">上架</el-button>
                                <el-button v-else-if="scope.row.publishStatus === 1" type="text" size="small" @click="handleProductDown(scope.row.id)">下架</el-button>
                                <el-button v-else-if="scope.row.publishStatus === 2" type="text" size="small" @click="handleProductUp(scope.row.id)">上架</el-button>
                                <el-button type="text" size="small" @click="handleUpdateBaseAttrVisible(scope.row)">规格</el-button>
                                
                            </div>
                        </template>
                    </el-table-column>
                </el-table>

                <!-- 分页工具栏 -->
                <div class="pagination">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="searchForm.page"
                            :page-sizes="[10, 20, 30, 50, 100, 200]"
                            :page-size="searchForm.limit"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="totalPage">
                    </el-pagination>
                </div>

                <!-- 规格属性维护 -->
                <SpuBaseAttrUpdate ref="SpuBaseAttr" v-if="visibleBaseAttr"></SpuBaseAttrUpdate>
                
            </el-card>
        </div>
    </div>
</template>

<script>
import { save, deleted, updated, info, list, page, selectMyPage, querySupInfoPage } from "@/api/product/pmsspuinfo"
import CategoryCascader from "@/views/product/components/category-cascader";
import BrandSelect from "@/views/product/components/brand-select";
import SpuBaseAttrUpdate from "./spu_base_attr_update";

export default {
    components: {
        CategoryCascader, BrandSelect, SpuBaseAttrUpdate
    },
    data() {
        return {
            visibleBaseAttr: false,
            searchVisible: false,
            submitLoading: false,

            dataList: [],
            totalPage: 0,
            dataListLoading: true,
            selectionRows: [],

            searchForm: {
                page: 1,
                limit: 10,
                sortColumns: 'ctime,utime',
                sortType: 'desc',
                keyword: null,
                cateId: null,
                brandId: null
            },
            formBaseAttr: {
                id: null,
                cateId: null,
                brandId: null,
                spuName: null,
                spuDescription: null,
                weight: null,
                publishStatus: null,
                ctime: null,
                utime: null
            },
            rulesBaseAttr: {
            },

            categoryPath: [],
            catePathSub: null,
            brandIdSub: null,


            dataResp: {
                attrGroups: [],
                baseAttrs: []
            },
            spuAttrsMap: {}
        }
    },
    mounted() {
    	this.$nextTick(function () {
            this.catePathSub = PubSub.subscribe("catePathId", (msg, val) => {
                this.searchForm.cateId = val[val.length-1];
            });
            this.brandIdSub = PubSub.subscribe("brandId", (msg, val) => {
                this.searchForm.brandId = val;
            });

	        this.getDataList()
    	})
    },
    methods: {
        /**
         * 规格属性维护
         */
		handleUpdateBaseAttrVisible(row) {
            this.visibleBaseAttr = true
            this.$nextTick(() => {
                this.$refs.SpuBaseAttr.init(row.cateId, row.id);
            });
        },

        /**
         * 商品上架
         */
        handleProductUp(spuId) {
            this.$confirm('确定要上架商品？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
                this.$http({
                    url: `/product/pmsspuinfo/${spuId}/up`,
                    method: 'get'
                }).then(res => {
                    if (this.ResultMessage(res)) {
                        this.getDataList()
                    }
                })
            }).catch(() => {})
        },

        /**
         * 商品下架
         */
        handleProductDown(spuId) {
            this.$confirm('确定要下架商品？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
                this.$http({
                    url: `/product/pmsspuinfo/${spuId}/down`,
                    method: 'get'
                }).then(res => {
                    if (this.ResultMessage(res)) {
                        this.getDataList()
                    }
                })
            }).catch(() => {})
        },

		/**
         * 删除
         */
        handleDelete(row) { 
            let ids = []
            if (row.id != undefined) {
                ids.push(row.id)
            }
            if(this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({ title: '提示', message: '请选择需要删除的记录！', type: 'info' });
            } else {    
                this.$confirm('确定要删除？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
                    deleted(ids).then(res => {
                        if (this.ResultMessage(res)) {
                            this.getDataList()
                        } 
                    }).catch(error => {
                        this.$notify({ title: '错误', message: error, type: 'error' });
                    })
                }).catch(() => {})
            }
        },

		/**
         * 详细信息
         */
        getInfo(id) {
            if (id !== undefined) {
                return info(id).then(res => {
                    if (this.ResultMessage(res, false)) {
                        return res
                    } else {
                        this.$notify({ title: '错误', message: error, type: 'error' });
                    }
                }).catch(error => {
                    this.$notify({ title: '错误', message: error, type: 'error' });
                })
            }
        },
		
        /**
         * 数据列表
         */
        getDataList() {
            querySupInfoPage(this.searchForm).then(res => {
                if (this.ResultMessage(res, false)) {
                    this.dataList = res.data.records
                    this.totalPage = res.data.total
                    this.dataListLoading = false
                } else {
                    this.dataListLoading = false
                }
            }).catch(error => {
                this.dataListLoading = false
                this.$notify({ title: '错误', message: error, type: 'error' });
            })
        },

        handleSelection(selection) { // 多选
            this.selectionRows = selection
        },
        handleSizeChange(val) { // 每页数量
            this.searchForm.limit = val
            this.getDataList()
        },
        handleCurrentChange(val) { // 页码
            this.searchForm.page = val
            this.getDataList()
        },
        handleResetSearch(name) { // 重置搜索表单
            this.handleReset(name)
            this.$refs.brand.brandId = null
            this.$refs.category.paths = []

            this.getDataList()
        },
        handleReset(name) { // 重置表单
            const refs = this.$refs
            refs[name].resetFields()
        },
        handleSearch() { // 单条件搜索
            this.getDataList()
        }
        
    },

    /**
     * 生命周期 - 销毁之前
     */
    beforeDestroy() {
        PubSub.unsubscribe(this.catePathSub); 
        PubSub.unsubscribe(this.brandIdSub); 
    }
}
</script>